﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace QGZX.Logic.Cwy
{
    public class EnrollmentController : Controller
    {
        #region 加载乘务员报名信息视图--ActionResult Index()
        /// <summary>
        /// 加载乘务员报名信息视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获取乘务员cwy_enroll报名数据 +GetEnrollData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取乘务员cwy_volunteer表格数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetEnrollData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetPagedList(pageIndex, pageSize, x => x.EnrollId != null, m => m.StudentNum).Select(x => x.ToPOCO());
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改乘务员报名信息表的 window -- ActionResult EditEnroll（）
        /// <summary>
        /// 加载修改乘务员报名信息的 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditEnroll(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改乘务员报名信息表的 window EditEnroll(MODEL.qgzx_cwy_enroll model)
        /// <summary>
        ///post修改乘务员概要信息表的 EditGeneral(MODEL.qgzx_company model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditEnroll(MODEL.qgzx_cwy_enroll model)
        {
            model.EnrollTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.Modify(model, "StudentNum", "StudentName", "IsAgree", "IsWorked", "BeHeadman", "IsTakeZMJH", "EnrollTime");
            if (res > 0)
            {
                return Redirect("/cwy/Enrollment/Index");
            }
            return Redirect("/cwy/Enrollment/Index");
        }
        #endregion

        #region 新增乘务员报名学生的window + AddEnroll()
        /// <summary>
        /// 新增乘务员报名学生的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddEnroll()
        {
            return PartialView();
        }
        #endregion

        #region 新增乘务员报名学生 html代码 + AddEnroll(MODEL.qgzx_cwy_general model)
        /// <summary>
        /// 新增乘务员报名学生 html代码 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddEnroll(MODEL.qgzx_cwy_enroll model)
        {
            model.EnrollTime = System.DateTime.Now; 
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/cwy/Enrollment/Index?ok");
            }
            return Redirect("/cwy/Enrollment/Index?err");
        }
        #endregion

        #region 删除乘务员报名学生信息 + DelEnroll()
        /// <summary>
        /// 删除乘务员报名学生信息 + DelEnroll()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelEnroll()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.DelBy(x => x.EnrollId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了,您现在删除的学生正在操作" + ex, null, "");
            }
        }
        #endregion

        #region 查找报名学生的详细信息--ActionResult EnrollDetail(int id)
        /// <summary>
        /// 查找报名学生的详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EnrollDetail(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId == id).FirstOrDefault().ToViewModel();
            var list = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentNum == model.StudentNum).FirstOrDefault().ToViewModel();
            if (list != null)
            {
                int Gender = list.Gender;
                int Grade = (int)list.Grade;
                int Height = (int)list.Height;
                string Dormitory = list.Dormitory;
                int PoliticStatus = (int)list.PoliticStatus;
                string College = OperateContext.Current.BLLSession.Iqgzx_collegeBLL.GetListBy(x => x.CollegeId == list.College).FirstOrDefault().CollegeName;
                string Profession = OperateContext.Current.BLLSession.Iqgzx_professionBLL.GetListBy(x => x.ProfessionId == list.Profession).FirstOrDefault().ProfessionName;
                if (Gender == 0)
                {
                    ViewBag.Gender = "男";
                }
                else
                {
                    ViewBag.Gender = "女";
                }
                if (PoliticStatus == 0) { ViewBag.PoliticStatus = "中共党员"; }
                if (PoliticStatus == 1) { ViewBag.PoliticStatus = "预备党员"; }
                if (PoliticStatus == 2) { ViewBag.PoliticStatus = "共青团员"; }
                if (PoliticStatus == 3) { ViewBag.PoliticStatus = "群众"; }
                else { ViewBag.PoliticStatus = "其他党派"; }
                ViewBag.Height = Height;
                ViewBag.Dormitory = Dormitory;
                ViewBag.Grade = Grade;
                ViewBag.College = College;
                ViewBag.Profession = Profession;
            }
            return PartialView(model);
        }
        #endregion

        #region 登记报名学生的面试成绩 window -- ActionResult InterviewScore（）
        /// <summary>
        /// 登记报名学生的面试成绩 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult InterviewScore(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post登记报名学生的面试成绩 window InterviewScore(MODEL.qgzx_cwy_enroll model)
        /// <summary>
        ///post登记报名学生的面试成绩 EditGeneral(MODEL.qgzx_cwy_enroll model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult InterviewScore(MODEL.qgzx_cwy_enroll model)
        {
            model.EnteringTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.Modify(model, "Interviewer", "ViewComment", "ViewScore", "ViewEntering", "EnteringTime");
            if (res > 0)
            {
                return Redirect("/cwy/Enrollment/Index");
            }
            return Redirect("/cwy/Enrollment/Index");
        }
        #endregion

        #region 登记报名学生的培训考核成绩 window -- ActionResult TrainScore（）
        /// <summary>
        /// 登记报名学生的培训考核成绩 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult TrainScore(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post登记报名学生的培训考核成绩 window TrainScore(MODEL.qgzx_cwy_enroll model)
        /// <summary>
        ///post登记报名学生的培训考核成绩 EditGeneral(MODEL.qgzx_company model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult TrainScore(MODEL.qgzx_cwy_enroll model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.Modify(model, "TrainAbsent", "TrainScore", "PhysicalCheck", "FinalScore");
            if (res > 0)
            {
                return Redirect("/cwy/Enrollment/Index");
            }
            return Redirect("/cwy/Enrollment/Index");
        }
        #endregion

        #region 最终审核，确认志愿者名单 window -- ActionResult FinalAudition（）
        /// <summary>
        ///  最终审核，确认志愿者名单  window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult FinalAudition(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post 最终审核，确认志愿者名单  window FinalAudition(MODEL.qgzx_cwy_enroll model)
        /// <summary>
        ///post 最终审核，确认志愿者名单  EditGeneral(MODEL.qgzx_company model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult FinalAudition(MODEL.qgzx_cwy_enroll model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.Modify(model, "IsAgree", "IsWorked", "BeHeadman", "IsTakeZMJH", "Interviewer", "ViewComment", "ViewScore", "ViewEntering", "TrainAbsent", "TrainScore", "PhysicalCheck", "FinalScore", "IsPassed", "Reason");
            if (res > 0)
            {
                return Redirect("/cwy/Enrollment/Index");
            }
            return Redirect("/cwy/Enrollment/Index");
        }
        #endregion

        #region 对审核通过的学生进行初步分组 window -- ActionResult Grouping（）
        /// <summary>
        /// 对审核通过的学生进行初步分组 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult Grouping(int id)
        {
            //根据EnrollId添加学生到 volunteer表
            MODEL.ViewModel.CwyEnroll enroll = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId == id).FirstOrDefault().ToViewModel();
            MODEL.ViewModel.CywVolunteer volunteer = new MODEL.ViewModel.CywVolunteer();
            if (enroll != null)
            {
                var model = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.GetListBy(x => x.StudentNum == enroll.StudentNum && x.GeneralId == enroll.GeneralID);
                if(model.Count() == 0)
                {
                    volunteer.StudentNum = enroll.StudentNum;
                    volunteer.StudentName = enroll.StudentName;
                    volunteer.GeneralId = enroll.GeneralID;
                }
                else
                {
                    return Redirect("/cwy/Enrollment/Index");
                }
            }
            //将权限对象 传给视图的model属性
            return PartialView(volunteer);
        }
        #endregion

        #region post对审核通过的学生进行初步分组 window Grouping(MODEL.qgzx_cyw_volunteer model)
        /// <summary>
        ///post对审核通过的学生进行初步分组 EditGeneral(MODEL.qgzx_company model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult Grouping(MODEL.qgzx_cwy_volunteer model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.Add(model);
            if (res > 0)
            {
                return Redirect("/cwy/Enrollment/Index");
            }
            return Redirect("/cwy/Enrollment/Index");
        }
        #endregion

        #region 搜索方法 --ActionResult Search()
        /// <summary>
        /// 多条件搜索
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string StudentNum = Request.QueryString["StudentNum"];
            string StudentName = Request.QueryString["StudentName"];
            string Interviewer = Request.QueryString["Interviewer"];
            string ViewComment = Request.QueryString["ViewComment"];
            string ViewEntering = Request.QueryString["ViewEntering"];
            string Reason = Request.QueryString["Reason"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！

            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.EnrollId != null).ToList().Select(x => x.ToPOCO());
            if (StudentNum != "")
            {
                list = list.Where(x => x.StudentNum.Contains(StudentNum)).ToList();
            }
            if (StudentName != "")
            {
                list = list.Where(x => x.StudentName.Contains(StudentName)).ToList();
            }
            if (Interviewer != null)
            {
                list = list.Where(x => x.Interviewer.Contains(Interviewer)).ToList();
            }
            if (ViewComment != "")
            {
                list = list.Where(x => x.ViewComment.Contains(ViewComment)).ToList();
            }
            if (ViewEntering != "")
            {
                list = list.Where(x => x.ViewEntering.Contains(ViewEntering)).ToList();
            }
            if (Reason != "")
            {
                list = list.Where(x => x.Reason.Contains(Reason)).ToList();
            }

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
